<template>
	<view>
		<cu-custom :isBack="true">
			<block slot="backText"></block>
		</cu-custom>
		<form>
			<view class="cu-form-group">
				<view class="title">{{$t('type')}}</view>
				<picker @change="typeChange" :value="typeIndex" :range="typeNameList" disabled>
					<view class="picker">
						{{typeNameList[typeIndex]}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group" v-for="(describe,describeIndex) in workOrder.describeList" :key="describeIndex">
				<view class="title">{{describe.name}}</view>
				<input v-model="describe.value" disabled />
			</view>
			<view class="cu-form-group align-start margin-top">
				<view class="title">{{$t('problem')}}</view>
				<textarea maxlength="-1" v-model="workOrder.problem" disabled></textarea>
			</view>
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					{{$t('enclosure')}}
				</view>
				<view class="action">
					{{workOrder.enclosureList.length}}/4
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in workOrder.enclosureList" :key="index" @tap="ViewImage"
						:data-url="workOrder.enclosureList[index].downloadPath">
						<image :src="workOrder.enclosureList[index].downloadPath" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="workOrder.enclosureList.length<4">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
		</form>
	</view>
</template>

<script>
	import myConfig from '@/myConfig.js'
	import * as erpWorkOrderApi from '@/api/erp/erpWorkOrderApi.js'
	import * as erpWorkOrderTypeApi from '@/api/erp/erpWorkOrderTypeApi.js'
	import * as erpWorkOrderTypeDescribeKeyApi from '@/api/erp/erpWorkOrderTypeDescribeKeyApi.js'
	import * as erpWorkOrderTypeUserApi from '@/api/erp/erpWorkOrderTypeUserApi.js'
	import * as erpWorkOrderEnclosureApi from '@/api/erp/erpWorkOrderEnclosureApi.js'
	import * as storage from '@/util/storage.js'
	export default {
		data() {
			return {
				typeIndex: 0,
				userIndex: 0,
				userId: storage.getUserId(),
				companyId: storage.getCompanyId(),
				workOrder: {
					describeList: [],
					enclosureList: [],
				},
				typeList: [],
				typeNameList: [],

			}
		},
		computed: {
			themeConfig() {
				return this.$store.state.themeConfig
			}
		},
		onLoad(option) {
			this.workOrderId = option.workOrderId
			this.listWorkOrderType()
		},
		methods: {
			getWorkOrder() {
				erpWorkOrderApi.getWorkOrder({
					workOrderId: this.workOrderId
				}).then(res => {
					const {
						data
					} = res
					if (data) {
						this.workOrder = data
						for (let i in this.typeList) {
							const type = this.typeList[i]
							if (type.typeId === data.typeId) {
								this.typeIndex = i
							}
						}
					}
				})
			},
			typeChange(e) {
				this.typeIndex = e.detail.value
				this.workOrder.typeId = this.typeList[this.typeIndex].typeId
			},
			listWorkOrderType() {
				erpWorkOrderTypeApi.listWorkOrderType({
					companyId: this.companyId
				}).then(res => {
					const {
						data
					} = res
					if (data) {
						this.typeList = data
						const typeNameList = []
						data.forEach(item => {
							typeNameList.push(item.name)
						})
						this.typeNameList = typeNameList
					}
					this.getWorkOrder()
				})
			},
			ChooseImage() {
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						const filePaths = res.tempFilePaths
						filePaths.forEach(filePath => {
							uni.uploadFile({
								url: myConfig.app.baseUrl +
									'/erp/work/order/enclosure/uploadAndGet',
								filePath: filePath,
								header: {
									'access-token': storage.getToken()
								},
								formData: this.workOrder,
								name: 'file',
								success: (uploadFileRes) => {
									const res = JSON.parse(uploadFileRes.data)
									const {
										data
									} = res
									if (data) {
										this.workOrder.enclosureList.push({
											enclosureId: data.enclosureId,
											downloadPath: filePath
										})
									}
								},
								fail(e) {
									console.log(e)
								}
							});
						})
					}
				});
			},
			ViewImage(e) {
				const urls = []
				this.workOrder.enclosureList.forEach(item => {
					urls.push(item.downloadPath)
				})
				uni.previewImage({
					urls: urls,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					content: this.$t('delete'),
					cancelText: this.$t('cancel'),
					confirmText: this.$t('confirm'),
					success: res => {
						if (res.confirm) {
							const enclosure = this.workOrder.enclosureList[e.currentTarget.dataset.index]
							erpWorkOrderEnclosureApi.deleteWorkOrderEnclosure({
								enclosureId: enclosure.enclosureId
							})
							this.workOrder.enclosureList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
		}
	}
</script>

<style>

</style>
